﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GrainManage.Entities.Contact;
using GrainManage.Proxy;

namespace GrainManage.Controls
{
    /// <summary>
    /// Interaction logic for EditContact.xaml
    /// </summary>
    public partial class EditContact : UserControl
    {
        public EditContact()
        {
            InitializeComponent();
        }

        private GrainManage.Entities.Table.Contact GetContactInfo()
        {
            var contact = new GrainManage.Entities.Table.Contact();
            contact.Address = boxAddress.Text;
            contact.Area = comboArea.Text;
            contact.BirthDate = Convertor.ToDateTime(boxBirth.Text);
            contact.Cellphone = boxCell.Text;
            contact.ContactID = Convert.ToInt32(txtId.Text);
            contact.ContactName = boxName.Text;
            contact.Creator = App.Authentication.UserName;
            contact.Email = boxEmail.Text;
            contact.Gender = (rbMale.IsChecked.Value ? rbMale.Content : rbFemale.Content) as string;
            //contact.ImageName = System.IO.Path.GetFileName(boxPath.Text);
            contact.QQ = boxQQ.Text;
            contact.Remarks = boxRemark.Text;
            contact.Telephone = boxTele.Text;
            return contact;
        }

        public void SetContactInfo(GrainManage.Entities.Contact.ViewGetContactsWithPhoto contact)
        {
            boxAddress.Text = contact.Address;
            comboArea.Text = contact.Area;
            boxBirth.Text = contact.BirthDate.Value.ToLongDateString();
            boxCell.Text = contact.Cellphone;
            txtId.Text = contact.ContactID.ToString();
            boxName.Text = contact.ContactName;
            //contact.Creator = App.Authentication.UserName;
            boxEmail.Text = contact.Email;
            if (contact.Gender == rbMale.Content as string)
            {
                rbMale.IsChecked = true;
            }
            else
            {
                rbFemale.IsChecked = true;
            }
            img.Source = Convertor.ToImage(contact.Photo);
            //img.Source = new BitmapImage(new Uri(contact.Photo));
            //boxPath.Text = contact.ImageName;
            boxQQ.Text = contact.QQ;
            boxRemark.Text = contact.Remarks;
            boxTele.Text = contact.Telephone;

        }

        private void btnScan_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = AppSettings.ImageFilter;
            var isDisplayed = dlg.ShowDialog();
            if (isDisplayed.HasValue && isDisplayed.Value)
            {
                boxPath.Text = dlg.FileName;
                var bitmap = new BitmapImage(new Uri(boxPath.Text, UriKind.Absolute));
                img.Source = bitmap;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var contact = GetContactInfo();
            btnSave.IsEnabled = false;
            if (contact.ContactID == 0)
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    var input = new InputInsert();
                    input.Contact = contact;
                    input.Authentication = App.Authentication;
                    return Creator.ContactProxy.Insert(input);
                },
               (result) =>
               {
                   btnSave.IsEnabled = true;
               },
               (result) =>
               {
                   if (result.Status.StatusCode != 1)
                   {
                       SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                   }
                   else
                   {
                       txtId.Text = result.ContactID.ToString();
                   }
               }, Dispatcher);
            }
            else
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    var input = new InputUpdate();
                    input.Contact = contact;
                    input.Authentication = App.Authentication;
                    return Creator.ContactProxy.Update(input);
                },
             (result) =>
             {
                 btnSave.IsEnabled = true;
             },
             (result) =>
             {
                 if (result.Status.StatusCode != 1)
                 {
                     SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                 }
             }, Dispatcher);
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            SetContactInfo(new ViewGetContactsWithPhoto { ContactID = Convert.ToInt32(txtId.Text) });
        }
    }
}
